package ecom.nopecom.util;

import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.RemoteWebDriver;

public class NopComFactory {

	public static NopeLogger LOGGER = getLogger(NopComFactory.class);

    public static WebDriver mBrowser;


	public static NopeLogger getLogger(Class clazz) {
		 return NopeLogger.getLogger(clazz);
		}
	public static WebDriver getBrowser() {
		if(mBrowser==null)
            return getFirefoxBrowser();
        else
		return mBrowser;
	}
	 public static WebDriver getFirefoxBrowser(){//String version) {
	        LOGGER.info("getFirefoxBrowser()");
	      //  WebDriver mBrowser = null;

	        try {
	            String remoteValue = "true";
	        	//System.getProperty("remote");
	            if (remoteValue != null && remoteValue.equalsIgnoreCase("true")) {
	                LOGGER.info("Getting remote firefox web driver");

//                    System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver.exe");
	                DesiredCapabilities firefox = DesiredCapabilities.chrome();
	             //   firefox.setVersion(version);

//	                firefox.setPlatform(Platform.ANY);
//	                firefox.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
//	                mBrowser = new ChromeDriver();
                   mBrowser = new  RemoteWebDriver(new URL("http://cb_ram-core:2c259106-416c-4890-9e0a-9f09ccb96c74@ondemand.saucelabs.com:80/wd/hub"), firefox);

	                //Uncomment line below to enable remote testing from your build machine with VM images
	                //mBrowser = new RemoteWebDriver(new URL("http://9.79.12.251:4444/wd/hub"), DesiredCapabilities.firefox());

	                mBrowser.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
	                mBrowser.get("http://demo.nopcommerce.com");
	                //mBrowser.manage().window().setPosition(new Point(0,0));
	                //java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
	                //Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
	                //mBrowser.manage().window().setSize(dim);

	            } 
	            else 
	            {
	                LOGGER.info("Preparing local firefox profile");
	            //    File file = JacuzziResources.getFile("firebug/firebug-1.9.2.xpi");
	                FirefoxProfile firefoxProfile = new FirefoxProfile();
	            //    firefoxProfile.addExtension(file);

	                // Reference: http://getfirebug.com/wiki/index.php/Firebug_Preferences
	                firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.9.2");  // Avoid startup screen
	                firefoxProfile.setPreference("extensions.firebug.script.enableSites", true);
	                firefoxProfile.setPreference("extensions.firebug.console.enableSites", true);
	                firefoxProfile.setPreference("extensions.firebug.allPagesActivation", true);
	                firefoxProfile.setPreference("extensions.firebug.delayLoad", false);


//	                //if (JacuzziProperties.getBooleanSystemProperty(JacuzziSystemProperty.USE_PROXY)) {
//	                    firefoxProfile.setPreference("network.proxy.type", 1);
//	                    firefoxProfile.setPreference("network.proxy.http", AutomationConstants.PROXY_HOST);
//	                    firefoxProfile.setPreference("network.proxy.http_port", AutomationConstants.PROXY_PORT);
//	                    firefoxProfile.setPreference("network.proxy.ssl", AutomationConstants.PROXY_HOST);
//	                    firefoxProfile.setPreference("network.proxy.ssl_port", AutomationConstants.PROXY_PORT);
//	                    //GA: Added proxy ignore list
//	                    firefoxProfile.setPreference("network.proxy.no_proxies_on", AutomationConstants.PROXY_HOST_IGNORE);
//	                //}

	                LOGGER.info("Getting local firefox web driver");
	                mBrowser = new FirefoxDriver(firefoxProfile);
	                mBrowser.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

	                mBrowser.manage().window().setPosition(new Point(0, 0));
	                java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
	                Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
	                mBrowser.manage().window().setSize(dim);
	            }


	        } catch (Exception e) {
	        	System.out.print("Could not start browser for reason " + e.getMessage());
	           // throw new JacuzziConfigurationException("could not start Browser Factory", e);
	        }
	        return mBrowser;
	 
	 }
	  public static class ScreenShotRemoteWebDriver extends RemoteWebDriver implements TakesScreenshot {
	        public ScreenShotRemoteWebDriver(URL url, DesiredCapabilities dc) {
	            super(url, dc);
	        }
	        public <X> X getScreenshotAs(OutputType<X> target)
	                throws WebDriverException {
	            if ((Boolean) getCapabilities().getCapability(CapabilityType.TAKES_SCREENSHOT)) {
	                return target.convertFromBase64Png(execute(DriverCommand.SCREENSHOT).getValue().toString());
	            }
	            return null;
	        }
	  }
	

}
